#include "shawrapper.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "encyptwrapper/libmd-0.3/sha.h"
#ifdef __cplusplus
}
#endif


unsigned char * _SHAFile(FILE *fp, unsigned char *digest)
{
    unsigned char buf[1024];
    SHA_CTX ctx;
    int n;
    unsigned char *ret = digest;

    SHAInit(&ctx);
    while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
        SHAUpdate(&ctx, buf, n);
    SHAFinal(digest, &ctx);
    if (ferror(fp))
        ret = NULL;
    return ret;
}


unsigned char * _SHAFile(const char *path, unsigned char *digest)
{
    unsigned char *ret;
    FILE *fp = fopen(path, "rb");
    if(fp){
        ret = _SHAFile(fp, digest);
        fclose(fp);
    }else{
        perror(path);
        ret = NULL;
    }
    return ret;
}
